/*
 * lisenses
 * 
 */

package com.openjawtech.xscraper.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import com.openjawtech.xscraper.schema.FlightsType;
import com.openjawtech.xscraper.web.dto.FlightsDTO;
import com.openjawtech.xscraper.web.service.FlightsService;
import com.openjawtech.xscraper.web.util.TimeUtil;
import com.openjawtech.xscraper.ws.FlightsServiceStub;
import com.openjawtech.xscraper.ws.xsd.GetAllArrivalsDocument;
import com.openjawtech.xscraper.ws.xsd.GetAllArrivalsResponseDocument;
import com.openjawtech.xscraper.ws.xsd.SearchDocument;
import com.openjawtech.xscraper.ws.xsd.SearchResponseDocument;
import java.rmi.RemoteException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Date;
import java.util.Calendar;

/**
 *
 * 26/02/2009
 * @author Wenbo.Sun
 */
public class FlightsServiceImpl implements FlightsService {

    @Autowired
    private FlightsServiceStub serviceStub;

    private int preDays;
    private int nextDays;

    private static final Log log = LogFactory.getLog(FlightsService.class);

    public FlightsServiceImpl() {
        this.preDays = 0;
        this.nextDays = 0;
    }

    public FlightsServiceImpl(int preDays, int nextDays) {
        this.preDays = preDays;
        this.nextDays = nextDays;
    }


    public String[] getAllAirports() {
        String[] names = null;
        try {
            names = serviceStub.getAllAirports().getGetAllAirportsResponse().getReturnArray();
        } catch (RemoteException ex) {
            log.error("web service get all airports failed", ex);
            throw ex;
        } finally{
            return names;
        }
    }

    public String[] getAllArrivals(String departure) {
        String[] names = null;
        try {
            GetAllArrivalsDocument getAllArrivalsDoc = GetAllArrivalsDocument.Factory.newInstance();
            GetAllArrivalsDocument.GetAllArrivals getAllArrivals= getAllArrivalsDoc.addNewGetAllArrivals();
            getAllArrivals.setDeparture(departure);
            GetAllArrivalsResponseDocument getAllArrResDoc = serviceStub.getAllArrivals(getAllArrivalsDoc);
            names = getAllArrResDoc.getGetAllArrivalsResponse().getReturnArray();
        } catch (RemoteException ex) {
            log.error("web service get all arrivals failed", ex);
            throw ex;
        } finally{
            return names;
        }
    }

    public FlightsDTO searchFlights(String departure, String arrival, Date date) {
        FlightsDTO flights = null;
        log.debug("start searching ...");
        try {
            SearchDocument searchDoc = SearchDocument.Factory.newInstance();
            FlightsType flightsType = searchDoc.addNewSearch().addNewFlights();
            flightsType.setArrival(arrival);
            flightsType.setDeparture(departure);
            Calendar travelDate = TimeUtil.preOrNextCalendar(TimeUtil.newCalendar(date), this.preDays - 1);
            for(int j = this.preDays ; this.preDays <= j && j <= this.nextDays; j++)
                flightsType.addNewFlight().setTravelDate(TimeUtil.preOrNextCalendar(travelDate, 1));
            SearchResponseDocument searchResDoc = serviceStub.search(searchDoc);
            flights = new FlightsDTO(searchResDoc.getSearchResponse().getFlights());
        } catch (RemoteException ex) {
            log.error("web service get all arrivals failed", ex);
            throw ex;
        } finally{
            return flights;
        }
    }

}
